/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */
 
 package org.ceroproject.ui.commandline.commands;

import java.util.ArrayList;
import java.util.List;

import org.ceroproject.games.Action;
import org.ceroproject.games.ActionEvent;
import org.ceroproject.games.ActionException;
import org.ceroproject.games.Card;
import org.ceroproject.games.Game;
import org.ceroproject.games.Zone;
import org.ceroproject.games.base.ActionPlayYourCards;
import org.ceroproject.ui.commandline.Command;
import org.ceroproject.ui.commandline.CommandLineLauncher;


/**
 * Actiont to play cards
 * 
 * @author Camille Roux
 */
public class PlayCards extends Command {
	private static PlayCards instance = new PlayCards();

	protected PlayCards() {
		super("play");
	}

	@Override
	public String toString() {
		return "play cards";
	}

	public static PlayCards getCommand() {
		return instance;
	}

	@Override
	public boolean executer(String parametres) {
		String[] arg = parametres.split(":");
		Game game = CommandLineLauncher.getGame();
		Zone from;
		Zone to;
		List<Card> cards = new ArrayList<Card>();
		if (arg.length == 3) {
			String[] split = arg[0].split("-");
			if (split.length == 1)
				from = game.getZones().get(split[0]);
			else if (split.length == 2)
				try {
					from = game.getPlayers().get(Integer.valueOf(split[0]))
							.getZones().get(split[1]);
				} catch (NumberFormatException e) {
					System.out.println("Player number conversion error");
					return false;
				}
			else
				return false;

			split = arg[1].split("-");
			if (split.length == 1)
				to = game.getZones().get(split[0]);
			else if (split.length == 2)
				try {
					to = game.getPlayers().get(Integer.valueOf(split[0]))
							.getZones().get(split[1]);
				} catch (NumberFormatException e) {
					System.out.println("Player number conversion error");
					return false;
				}
			else
				return false;

			for (Card card : from.getSortedList()) {
				if (card.getCardId() == Integer.valueOf(arg[2])) {
					ActionPlayYourCards action = null;
					for (Action act : game.getActions()) {
						if (act instanceof ActionPlayYourCards) {
							//FIXME : et si y'a plusieurs ActionplayCards ? et bah plouf ! => il faut utiliser les modules  :)
							try {
								action = (ActionPlayYourCards) act.getClass()
										.newInstance();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					cards.add(card);
					try {
						action.playerActed(new ActionEvent(game,
								CommandLineLauncher.getCurrentPlayer(), from,
								to, cards));
					} catch (ActionException e) {
						System.out.println("error during playing cards");
					}
					game.registerAction(action);
					return true;
				}
			}

		}
		return false;
	}

	@Override
	public String help() {

		return "play cards\n"
				+ "syntax:\n"
				+ "play:[zonefrom]:[zonefrom]:cardid : play the card with the id 'cardid' from [zonefrom] to [zoneto]\n"
				+ "[zone...] = zone  :  represent the zone 'zone' of the game\n"
				+ "[zone...] = playernb-zone  :  represent the zone 'zone' of the player number 'playernb'\n";
	}

}